#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Compare(char *c, char *a, int* k)//这里直接将k的地址传过来，这样不用调用strlen函数了
//虽然跑不过LeetCode，但是代码应该是没问题的
{
    int i = 0;
    while (i < *k && a[i] != '\0')
    {
        if (c[i] != a[i])
        {
            *k = i;
            break;
        }
        else
        {
            i++;
        }
    }
}
char *longestCommonPrefix(char **a, int a_sz)
{
    if (a_sz == 0)
        return "";
    int sz = strlen(a[0]) < strlen(a[1]) ? strlen(a[0]) : strlen(a[1]);
    char *c = malloc(sizeof(char) * sz + 1);
    int k = 0;
    for (int i = 0; i < sz; i++)
    {
        if (a[0][i] == a[1][i])
        {
            c[k] = a[0][i];
            k++;
        }
        else
        {
            break;
        }
    }

    for (int j = 2; j < a_sz; j++)
    {
        Compare(c, a[j], &k);
    }
    c[k] = '\0';
    return c;
}
int main()
{
    char *strs[] = {"feng", "fia", "fui"};
    int n = sizeof(strs) / sizeof(strs[0]);
    char *prefix = longestCommonPrefix(strs, n);
    printf("The longest common prefix is: %s\n", prefix);
    return 0;
}